package com.swotplus.system.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.swotplus.common.core.MenuInfo;
import com.swotplus.common.utils.StringUtils;
import com.swotplus.system.sys.entity.SysMenu;
import com.swotplus.system.sys.mapper.SysMenuMapper;
import com.swotplus.system.sys.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author hugang
 * @since 2020-04-12
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
        @Autowired
        private SysMenuMapper  sysMenuMapper;

        /**
         * 查询顶级目录集合
         *
         * @return 所有顶级菜单信息
         */
        @Override
        public List<SysMenu> selectMenuAll(String userid) {
                List<SysMenu> menuList = null;
                //超级管理员 用户 拥有全部目录
                if (userid.equals("1"))
                {
                        QueryWrapper<SysMenu> sysMenuQueryWrapper=new QueryWrapper<>();
                        sysMenuQueryWrapper.eq("menu_type","D").orderByDesc("order_num");
                        menuList = sysMenuMapper.selectList(sysMenuQueryWrapper);
                }
                else
                {
                        menuList = sysMenuMapper.selectMenuAllByUserId(userid);
                }
                return menuList;
        }

        /**
         * 根据顶级目录获取所有子节点的目录及菜单信息
         * @param menuId
         * @param userId
         * @return
         */
        @Override
        public List<MenuInfo> selectMenusByUserId(String menuId, String userId)
        {
                List<SysMenu> menuList =  new LinkedList<SysMenu>();
                //超级管理员
                if (userId.equals("1"))
                {
                        menuList = sysMenuMapper.selectMenuNormalAll(menuId);
                }
                else
                {
                        Map<String ,Object> map=new HashMap<>();
                        //用户id
                        map.put("userId",userId);
                        //父节点菜单id
                        map.put("parentId",menuId);
                        menuList = sysMenuMapper.selectMenusByUserId(map);

                }
                return getChildPerms(menuList,userId);
        }

        /**
         * 根据用户ID查询权限
         *
         * @param userId 用户ID
         * @return 权限列表
         */
        @Override
        public Set<String> selectPermsByUserId(String userId) {
                List<String> perms = sysMenuMapper.selectPermsByUserId(userId);
                Set<String> permsSet = new HashSet<>();
                for (String perm : perms)
                {
                        if (StringUtils.isNotEmpty(perm))
                        {
                                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
                        }
                }
                return permsSet;
        }


        /**
         * 根据父节点的ID获取所有子节点
         *
         * @param list 分类表tring
         * @return String
         */
        public List<MenuInfo> getChildPerms(List<SysMenu> list,String userId)
        {
                List<MenuInfo> returnList = new ArrayList<MenuInfo>();
                for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext();)
                {
                        SysMenu t = (SysMenu) iterator.next();
                        MenuInfo m=new MenuInfo();
                        m.setTitle(t.getMenuName());
                        m.setTarget(t.getTarget());
                        m.setHref(t.getUrl());
                        m.setId(Integer.toString(t.getId()));
                        m.setIcon(t.getIcon());
                        // 超级管理员
                        if(userId.equals("1")){
                                // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
                                QueryWrapper<SysMenu> sysMenuQueryWrapper=new QueryWrapper<>();
                                sysMenuQueryWrapper.eq("parent_id",t.getId()).in("menu_type",new Object[]{"M","C"});
                                List<SysMenu> sysMenus=sysMenuMapper.selectList(sysMenuQueryWrapper);
                                if (sysMenus.size()>0)
                                {
                                        m.setChildren(getChildList(sysMenus,userId)); ;
                                }
                        }else{
                                Map<String ,Object> map=new HashMap<>();
                                //用户id
                                map.put("userId",userId);
                                //父节点菜单id
                                map.put("parentId",t.getId());
                                List<SysMenu> sysMenus = sysMenuMapper.selectMenusByUserId(map);
                                if(sysMenus.size()>0){
                                        m.setChildren(getChildList(sysMenus,userId)); ;
                                }
                        }


                        returnList.add(m);
                }
                return returnList;
        }


        /**
         * 得到子节点列表
         */
        private List<MenuInfo> getChildList(List<SysMenu> list,String userId)
        {
                List<MenuInfo> tlist = new ArrayList<MenuInfo>();
                Iterator<SysMenu> it = list.iterator();
                while (it.hasNext())
                {
                        SysMenu n = (SysMenu) it.next();
                        MenuInfo m=new MenuInfo();
                        m.setTitle(n.getMenuName());
                        m.setTarget(n.getTarget());
                        m.setHref(n.getUrl());
                        m.setId(Integer.toString(n.getId()));
                        m.setIcon(n.getIcon());
                        // 超级管理员
                        if(userId.equals("1")){
                                // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
                                QueryWrapper<SysMenu> sysMenuQueryWrapper=new QueryWrapper<>();
                                sysMenuQueryWrapper.eq("parent_id",m.getId()).in("menu_type",new Object[]{"M","C"});
                                List<SysMenu> sysMenus=sysMenuMapper.selectList(sysMenuQueryWrapper);
                                if (sysMenus.size()>0)
                                {
                                        m.setChildren(getChildList(sysMenus,userId));
                                }
                        }else{
                                Map<String ,Object> map=new HashMap<>();
                                //用户id
                                map.put("userId",userId);
                                //父节点菜单id
                                map.put("parentId",m.getId());
                                List<SysMenu> sysMenus = sysMenuMapper.selectMenusByUserId(map);
                                if(sysMenus.size()>0){
                                        m.setChildren(getChildList(sysMenus,userId)); ;
                                }
                        }
                          tlist.add(m);
                }
                return tlist;
        }





        /**
         * 根据角色ID查询菜单
         *
         * @param roleId 角色ID
         * @return 菜单列表
         */
        @Override
        public List<Map<String, Object>> roleMenuTreeData(String roleId)
        {
                List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
                List<SysMenu> menuList = sysMenuMapper.selectList(null);
                if (StringUtils.isNotNull(roleId))
                {
                        List<String> roleMenuList = sysMenuMapper.selectMenuTree(roleId);
                        trees = getTrees(menuList, true, roleMenuList, true);
                }
                else
                {
                        trees = getTrees(menuList, false, null, true);
                }
                return trees;
        }

        /**
         * 查询所有菜单
         *
         * @return 菜单列表
         */
        @Override
        public List<Map<String, Object>> menuTreeData()
        {
                List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
                List<SysMenu> menuList = sysMenuMapper.selectList(null);
                trees = getTrees(menuList, false, null, false);
                return trees;
        }

        /**
         * 根据菜单ID查询信息
         *
         * @param id 菜单ID
         * @return 菜单信息
         */
        @Override
        public SysMenu selectMenuById(String id) {
                return sysMenuMapper.selectMenuById(id);
        }

        /**
         * 对象转菜单树
         *
         * @param menuList 菜单列表
         * @param isCheck 是否需要选中
         * @param roleMenuList 角色已存在菜单列表
         * @param permsFlag 是否需要显示权限标识
         * @return
         */
        public List<Map<String, Object>> getTrees(List<SysMenu> menuList, boolean isCheck, List<String> roleMenuList,
                                                  boolean permsFlag)
        {
                List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
                for (SysMenu menu : menuList)
                {
                        Map<String, Object> deptMap = new HashMap<String, Object>();
                        deptMap.put("id", menu.getId());
                        deptMap.put("pId", menu.getParentId());
                        deptMap.put("name", transMenuName(menu, roleMenuList, permsFlag));
                        deptMap.put("title", menu.getMenuName());
                        if (isCheck)
                        {
                                deptMap.put("checked", roleMenuList.contains(menu.getId() + menu.getPerms()));
                        }
                        else
                        {
                                deptMap.put("checked", false);
                        }
                        trees.add(deptMap);
                }
                return trees;
        }

        public String transMenuName(SysMenu menu, List<String> roleMenuList, boolean permsFlag)
        {
                StringBuffer sb = new StringBuffer();
                sb.append(menu.getMenuName());
                if (permsFlag)
                {
                        sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + menu.getPerms() + "</font>");
                }
                return sb.toString();
        }

}
